/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ChatGUI;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author SKL
 */
public class Listener extends Thread{
    private int portNumberListen = 58343;
    private String hostName = "localhost";
    private String line = "";
    private BufferedReader in = null;
    private Socket kkSocket = null;
    
    @Override
    public void run(){
        initSocketConnection();
        initBufferedReader();
        
        try 
        {
            while ((line = in.readLine()) != null) 
            {
                GUI.getInstance().newMessage(line);
            }
        } 
        catch (IOException ex) 
        {
            Logger.getLogger(Listener.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
    public void initBufferedReader(){
        try {
            in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
        } catch (IOException ex) {
            Logger.getLogger(Listener.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
    public void initSocketConnection(){
        try {
            kkSocket = new Socket(hostName, portNumberListen);
        } catch (UnknownHostException ex) {
            Logger.getLogger(Listener.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Listener.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
